#### ======================================================================
#### Online appendix: Figures OA1 and OA2 (subgroup preference gaps)
#### ----------------------------------------------------------------------
#### Purpose:
####   Generate the two online appendix figures:
####     - Figure OA1: preference gap by age subgroup (dodged bar plot)
####     - Figure OA2: preference gap by gender subgroup (dodged bar plot)
####
####   Each figure shows one horizontal bar per subgroup for each country,
####   dodged side by side. Separate plots are produced for China and USA.
####   Countries are sorted by the full-sample ("all") preference gap so
####   the subgroup bars can be compared against a consistent baseline.
####
####   This script also produces Table OA2 (PG/PV with bootstrapped CIs)
####   as a formatted CSV, using the CI columns added to the aggregated
####   results by weighting.R.
####
#### Data:
####   data/complete_aggregated_results.csv
####   (must be regenerated by weighting.R to include CI columns)
####
#### Output:
####   output/figure_OA1_age_subgroups.pdf
####   output/figure_OA2_gender_subgroups.pdf
####   output/table_OA2_pg_pv_with_cis.csv
####
#### Notes:
####   Calibri is embedded via showtext + sysfonts using the Microsoft Word
####   font directory on macOS.
#### ======================================================================

suppressPackageStartupMessages({
  library(dplyr)
  library(tidyr)
  library(ggplot2)
  library(readr)
  library(sysfonts)
  library(showtext)
})

#### ----------------------------------------------------------------------
#### Paths
#### ----------------------------------------------------------------------

in_path <- "data/complete_aggregated_results.csv"
out_dir <- "output"
dir.create(out_dir, showWarnings = FALSE, recursive = TRUE)

if (!file.exists(in_path)) {
  stop("Input file not found: ", in_path)
}

#### ----------------------------------------------------------------------
#### Typography (Calibri on macOS)
#### ----------------------------------------------------------------------

base_family <- "Calibri"

calibri_dir <- "/Applications/Microsoft Word.app/Contents/Resources/DFonts"
calibri_files <- c("Calibri.ttf", "Calibrib.ttf", "Calibrii.ttf", "Calibriz.ttf")
missing_files <- calibri_files[!file.exists(file.path(calibri_dir, calibri_files))]

if (length(missing_files) > 0) {
  stop(
    "Calibri font files not found in: ", calibri_dir, "\n",
    "Missing: ", paste(missing_files, collapse = ", "), "\n",
    "Ensure Microsoft Word for Mac is installed at /Applications."
  )
}

font_add(
  family = base_family,
  regular = file.path(calibri_dir, "Calibri.ttf"),
  bold = file.path(calibri_dir, "Calibrib.ttf"),
  italic = file.path(calibri_dir, "Calibrii.ttf"),
  bolditalic = file.path(calibri_dir, "Calibriz.ttf")
)

showtext_auto(enable = TRUE)
showtext_opts(dpi = 300)

#### ----------------------------------------------------------------------
#### Plot styling
#### ----------------------------------------------------------------------

base_size_pt    <- 11
axis_text_x_pt  <- 9
axis_text_y_pt  <- 9
axis_title_pt   <- 10
legend_text_pt  <- 9
strip_text_pt   <- 11

#### ----------------------------------------------------------------------
#### Countries included
#### ----------------------------------------------------------------------

countries_to_include <- c(
  "Argentina", "Belarus", "Bolivia", "Brazil", "Burkina Faso",
  "Cameroon", "Colombia", "Croatia", "DRC", "Egypt", "Georgia",
  "Ghana", "Haiti", "Hungary", "Indonesia", "Kenya", "Lebanon",
  "Madagascar", "Mali", "Mexico", "Nigeria", "Peru", "Philippines",
  "Poland", "Romania", "Serbia", "Tunisia", "Turkey", "Uruguay",
  "Venezuela"
)

#### ----------------------------------------------------------------------
#### Load data and harmonize country names
#### ----------------------------------------------------------------------

complete_results <- read_csv(in_path, show_col_types = FALSE) %>%
  mutate(
    Country = recode(
      Country,
      "Democratic Republic of Congo" = "DRC",
      "\u00dcrkiye" = "Turkey",
      "T\u00fcrkiye" = "Turkey",
      "the Philippines" = "Philippines"
    )
  ) %>%
  filter(
    Country %in% countries_to_include,
    !is.na(Country),
    Country != ""
  )

#### ----------------------------------------------------------------------
#### Subgroup display labels
#### ----------------------------------------------------------------------

age_subgroups <- c(
  "age_18_29"  = "18\u201329",
  "age_30_49"  = "30\u201349",
  "age_50_59"  = "50\u201359",
  "age_60plus" = "60+"
)

gender_subgroups <- c(
  "men"   = "Men",
  "women" = "Women"
)

#### ----------------------------------------------------------------------
#### Color palettes
#### ----------------------------------------------------------------------
#### Age uses a sequential grey-to-dark palette so age groups are visually
#### ordered. Gender uses two distinct tones.

age_colors <- c(
  "18\u201329" = "#4292c6",
  "30\u201349" = "#2171b5",
  "50\u201359" = "#08519c",
  "60+"        = "#08306b"
)

gender_colors <- c(
  "Men"   = "#4292c6",
  "Women" = "#d94801"
)

#### ----------------------------------------------------------------------
#### Helper: prepare data for a dodged bar plot
#### ----------------------------------------------------------------------
#### Returns a long data frame with one row per country-subgroup, sorted
#### by the full-sample ("all") PG for consistent country ordering.

prep_subgroup_bars <- function(df, actor, subgroup_map) {
  actor <- match.arg(actor, c("China", "USA"))

  # Full-sample PG for country sort order
  all_pg <- df %>%
    filter(Method == "weighted", Subgroup == "all") %>%
    transmute(
      Country,
      Sort_Delta = if (actor == "China") as.numeric(China_Delta)
                   else as.numeric(USA_Delta)
    )

  df_w <- df %>%
    filter(Method == "weighted", Subgroup %in% names(subgroup_map))

  out <- if (actor == "China") {
    df_w %>%
      transmute(
        Country,
        Subgroup_Label = factor(subgroup_map[Subgroup], levels = subgroup_map),
        Delta          = as.numeric(China_Delta)
      )
  } else {
    df_w %>%
      transmute(
        Country,
        Subgroup_Label = factor(subgroup_map[Subgroup], levels = subgroup_map),
        Delta          = as.numeric(USA_Delta)
      )
  }

  # Sort countries by full-sample PG (ascending, so largest positive at top)
  country_order <- all_pg %>%
    arrange(Sort_Delta) %>%
    pull(Country)

  out %>%
    mutate(Country = factor(Country, levels = country_order))
}

#### ----------------------------------------------------------------------
#### Helper: dodged horizontal bar plot
#### ----------------------------------------------------------------------

make_dodged_bar_plot <- function(data, x_label, fill_colors) {
  ggplot(data, aes(x = Delta, y = Country, fill = Subgroup_Label)) +
    geom_col(position = position_dodge(width = 0.75), width = 0.7) +
    geom_vline(xintercept = 0, colour = "black", linewidth = 0.3) +
    scale_fill_manual(values = fill_colors, name = NULL) +
    labs(x = x_label, y = "") +
    theme_minimal(base_family = base_family, base_size = base_size_pt) +
    theme(
      axis.text.x     = element_text(size = axis_text_x_pt, colour = "black"),
      axis.text.y     = element_text(size = axis_text_y_pt, colour = "black"),
      axis.title.x    = element_text(size = axis_title_pt, colour = "black"),
      legend.text      = element_text(size = legend_text_pt, family = base_family),
      legend.position  = "top",
      legend.justification = "left",
      panel.grid.minor = element_blank(),
      panel.grid.major.y = element_blank()
    )
}

#### ======================================================================
#### Figure OA1: Preference gaps by age subgroup
#### ======================================================================

age_cn <- prep_subgroup_bars(complete_results, "China", age_subgroups)
age_us <- prep_subgroup_bars(complete_results, "USA",   age_subgroups)

plot_age_cn <- make_dodged_bar_plot(age_cn, "Preference gap (China)", age_colors)
plot_age_us <- make_dodged_bar_plot(age_us, "Preference gap (USA)",   age_colors)

#### Combine China and USA panels side by side.
if (requireNamespace("patchwork", quietly = TRUE)) {
  library(patchwork)
  fig_oa1 <- plot_age_cn + plot_age_us +
    plot_layout(ncol = 2, guides = "collect") &
    theme(legend.position = "top")
} else {
  fig_oa1 <- plot_age_cn
}

ggsave(
  filename = file.path(out_dir, "figure_OA1_age_subgroups.pdf"),
  plot     = fig_oa1,
  width    = 14,
  height   = 10,
  units    = "in",
  dpi      = 300,
  device   = grDevices::cairo_pdf
)

if (!requireNamespace("patchwork", quietly = TRUE)) {
  ggsave(
    filename = file.path(out_dir, "figure_OA1_age_subgroups_us.pdf"),
    plot     = plot_age_us,
    width    = 950 / 96,
    height   = 650 / 96,
    units    = "in",
    dpi      = 300,
    device   = grDevices::cairo_pdf
  )
}

#### ======================================================================
#### Figure OA2: Preference gaps by gender subgroup
#### ======================================================================

gen_cn <- prep_subgroup_bars(complete_results, "China", gender_subgroups)
gen_us <- prep_subgroup_bars(complete_results, "USA",   gender_subgroups)

plot_gen_cn <- make_dodged_bar_plot(gen_cn, "Preference gap (China)", gender_colors)
plot_gen_us <- make_dodged_bar_plot(gen_us, "Preference gap (USA)",   gender_colors)

if (requireNamespace("patchwork", quietly = TRUE)) {
  fig_oa2 <- plot_gen_cn + plot_gen_us +
    plot_layout(ncol = 2, guides = "collect") &
    theme(legend.position = "top")
} else {
  fig_oa2 <- plot_gen_cn
}

ggsave(
  filename = file.path(out_dir, "figure_OA2_gender_subgroups.pdf"),
  plot     = fig_oa2,
  width    = 14,
  height   = 10,
  units    = "in",
  dpi      = 300,
  device   = grDevices::cairo_pdf
)

if (!requireNamespace("patchwork", quietly = TRUE)) {
  ggsave(
    filename = file.path(out_dir, "figure_OA2_gender_subgroups_us.pdf"),
    plot     = plot_gen_us,
    width    = 950 / 96,
    height   = 650 / 96,
    units    = "in",
    dpi      = 300,
    device   = grDevices::cairo_pdf
  )
}

#### ======================================================================
#### Table OA2: PG/PV data with bootstrapped CIs (formatted CSV)
#### ======================================================================
#### Extract the weighted, full-sample results for the 30 analysis countries
#### and format them as a publication-ready table with preference gaps and
#### their bootstrapped 95% CIs.

table_oa2 <- complete_results %>%
  filter(Method == "weighted", Subgroup == "all") %>%
  transmute(
    Country,
    ISO3_code,
    N,

    China_PG       = round(China_Delta, 3),
    China_PG_CI_Lo = round(China_Delta_CI_Lo, 3),
    China_PG_CI_Hi = round(China_Delta_CI_Hi, 3),
    China_PV       = round(China_SD_Desired, 3),

    USA_PG       = round(USA_Delta, 3),
    USA_PG_CI_Lo = round(USA_Delta_CI_Lo, 3),
    USA_PG_CI_Hi = round(USA_Delta_CI_Hi, 3),
    USA_PV       = round(USA_SD_Desired, 3)
  ) %>%
  arrange(Country)

write_csv(table_oa2, file.path(out_dir, "table_OA2_pg_pv_with_cis.csv"))

cat("Saved:\n",
    file.path(out_dir, "figure_OA1_age_subgroups.pdf"), "\n",
    file.path(out_dir, "figure_OA2_gender_subgroups.pdf"), "\n",
    file.path(out_dir, "table_OA2_pg_pv_with_cis.csv"), "\n")
